import {Message} from "@arco-design/web-vue";
import {useMapStore} from "./index";
import {saveToStorage} from "./storage";

// 添加地址到列表
export const addAddress = (poi: any) => {
    const store = useMapStore();

    const exists = store.addressList.find(item => item.uid === poi.uid);
    if (exists) {
        Message.warning("该地址已存在");
        return;
    }

    // 使用默认分类和标签

    const newAddress = {
        uid: poi.uid,
        title: poi.title,
        address: poi.address,
        point: poi.point,
        status: store.defaultTagId,
        categoryId: store.defaultCategoryId,
        addTime: new Date().toLocaleString(),
    };

    store.addressList.push(newAddress);
    saveToStorage();
    Message.success("地址添加成功");

    // 如果是第一个地址，自动切换到地址列表tab
    if (store.addressList.length === 1) {
        store.setActiveTab("list");
    }

    // 从搜索结果中移除已添加的地址
    store.searchResults = store.searchResults.filter(item => item.uid !== poi.uid);
};

// 从列表中移除地址
export const removeAddress = (uid: string) => {
    const store = useMapStore();

    const index = store.addressList.findIndex(item => item.uid === uid);
    if (index > -1) {
        store.addressList.splice(index, 1);
        saveToStorage();
        Message.success("地址移除成功");
    }
};

// 更改地址标签
export const changeAddressTag = (uid: string, tagId: string) => {
    const store = useMapStore();

    const address = store.addressList.find(item => item.uid === uid);
    if (address) {
        address.status = tagId;
        saveToStorage();
    }
};

// 更改地址分类
export const changeAddressCategory = (uid: string, categoryId: string) => {
    const store = useMapStore();

    const address = store.addressList.find(item => item.uid === uid);
    if (address) {
        address.categoryId = categoryId;
        saveToStorage();
    }
};

// 批量添加地址
export const batchAddAddresses = () => {
    const store = useMapStore();

    if (store.selectedAddresses.length === 0) {
        Message.warning("请先选择要添加的地址");
        return;
    }

    let addedCount = 0;

    store.selectedAddresses.forEach(uid => {
        const poi = store.searchResults.find(item => item.uid === uid);
        if (poi) {
            const exists = store.addressList.find(item => item.uid === poi.uid);
            if (!exists) {
                const newAddress = {
                    uid: poi.uid,
                    title: poi.title,
                    address: poi.address,
                    point: poi.point,
                    status: store.defaultTagId,
                    categoryId: store.defaultCategoryId,
                    addTime: new Date().toLocaleString(),
                };
                store.addressList.push(newAddress);
                addedCount++;
            }
        }
    });

    if (addedCount > 0) {
        saveToStorage();
        Message.success(`成功添加 ${addedCount} 个地址`);

        // 如果之前没有地址，现在有了，自动切换到地址列表tab
        if (store.addressList.length === addedCount) {
            store.setActiveTab("list");
        }

        // 从搜索结果中移除已添加的地址
        store.searchResults = store.searchResults.filter(item => !store.selectedAddresses.includes(item.uid));
        store.selectedAddresses = [];
    } else {
        Message.warning("所选地址都已存在");
    }
};

// 获取标签显示名称
export const getTagName = (tagId: string) => {
    const store = useMapStore();

    const tag = store.addressTags.find(t => t.id === tagId);
    return tag ? tag.name : tagId;
};
